package com.aynu.admin.controller;

import com.aynu.admin.domain.CommonResponse;
import com.aynu.admin.domain.po.TAdmin;
import com.aynu.admin.filter.AuthenticationInterceptor;
import com.aynu.admin.service.base.ITAdminService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private ITAdminService adminService;

    @PostMapping("/login")
    public CommonResponse<String> login(@RequestBody TAdmin admin) {
        return adminService.login(admin.getUsername(), admin.getPassword());
    }

    @PostMapping("/register")
    public CommonResponse<Object> register(@RequestBody TAdmin admin) {
        String userPrincipal = AuthenticationInterceptor.getUserPrincipal();
        if(!"super_admin".equals(userPrincipal)){
            return CommonResponse.failed("没有权限");
        }
        return adminService.register(admin);
    }
    //修改自己的密码
    @PostMapping("/modify-password")
    public CommonResponse<Object> modifyPassword(@RequestBody TAdmin admin) {
        String userPrincipal = AuthenticationInterceptor.getUserPrincipal();
        return adminService.modifyPassword(userPrincipal, admin.getPassword());
    }

    @GetMapping("/list")
    public CommonResponse<List<TAdmin>> list() {
        String userPrincipal = AuthenticationInterceptor.getUserPrincipal();
        if(!"super_admin".equals(userPrincipal)){
            return CommonResponse.failed("没有权限");
        }
        return CommonResponse.success(adminService.list());
    }
//    根据用户名删除管理员
    @DeleteMapping("/del")
    public CommonResponse<Object> del(@RequestParam("username") String username) {
        String userPrincipal = AuthenticationInterceptor.getUserPrincipal();
        return adminService.deleteByUsername(userPrincipal,username);
    }
    @GetMapping("/verify-token")
    public CommonResponse<String> verifyToken() {
        String userPrincipal = AuthenticationInterceptor.getUserPrincipal();
        return CommonResponse.success(userPrincipal);
    }

}
